package work.mediway.mdm.controller.biz;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.base.model.BaseResponse;
import com.mediway.hos.log.annotation.OperLog;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.biz.entity.biz.SsConfig;
import work.mediway.mdm.biz.service.biz.SsConfigService;
import work.mediway.mdm.biz.vo.biz.SsConfigVO;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2020/11/27
 */
@Api(tags = "配置信息模块")
@ApiSupport(order = 0)
@Validated
@RequestMapping("config")
@RestController
@RequiredArgsConstructor
public class SsConfigController {

    private final SsConfigService configService;

    @OperLog(title = "新增配置信息", content = "新增配置信息")
    @OpsLog(value = "新增配置信息", type = OpsLogTypeEnum.INSERT)
    @PostMapping
    @ApiOperation(value = "新增配置信息")
    @ApiOperationSupport(order = 1)
    public BaseResponse<String> addConfig(SsConfig config) {
        config.setSscCode(null);
        this.configService.save(config);
        return BaseResponse.success();
    }


    @OpsLog(value = "查询配置信息", type = OpsLogTypeEnum.SELECT)
    @GetMapping
    @ApiOperation(value = "查询配置信息")
    @ApiImplicitParam(value = "代码,描述或者值", name = "param", paramType = "query")
    @ApiOperationSupport(order = 3)
    public BaseResponse<Page<SsConfigVO>> pageListConfig(String param, QueryRequest queryRequest) {
        Page<SsConfigVO> page = this.configService.pageListConfig(param, queryRequest);
        return ObjectUtil.isNotEmpty(page) ? BaseResponse.success( page) : BaseResponse.error();
    }

    @OperLog(title = "更新配置信息", content = "更新配置信息")
    @OpsLog(value = "更新配置信息", type = OpsLogTypeEnum.UPDATE)
    @PostMapping("update")
    @ApiOperation(value = "更新配置信息", notes = "更新时需要将之前的数据一起传入,否则会丢失之前的数据")
    @ApiOperationSupport(order = 5)
    public BaseResponse<String> updateConfig(@Valid SsConfig config) {
        LambdaUpdateWrapper<SsConfig> updateWrapper = Wrappers.lambdaUpdate(SsConfig.class);
        updateWrapper.eq(SsConfig::getSscCode, config.getSscCode());
        this.configService.update(config, updateWrapper);
        return BaseResponse.success();
    }

}
